/**
 * @author luther.seven@gmail.com
 */

var camerasXHR;
var results;
var imgTitle;
var camId;

// XML Namespace resolver for XPath namespace resolution
function NSResolver(prefix) {
  var ns = {
    'georss' : 'http://www.georss.org/georss',
    'aws': 'http://www.aws.com/aws',
  };
  return ns[prefix] || null;
}

// Takes a definition document and populates the various DCTK controls
function handleCamerasResult(doc)
{
    // extract information with XPath
	
	results = doc.evaluate("count(//aws:camera)", doc, NSResolver, XPathResult.ANY_TYPE, null).numberValue;
	
	camId = new Array()
	for (var i = 1; i <= results; i++){
		camId[i] = doc.evaluate("//aws:camera[" + i + "]/@id", doc, NSResolver, XPathResult.STRING_TYPE, null).stringValue;
		}
	
	imgTitle = new Array()
	for (var i = 1; i <= results; i++){
		imgTitle[i] = doc.evaluate("//aws:camera[" + i + "]/@name", doc, NSResolver, XPathResult.STRING_TYPE, null).stringValue;
		}
    
    var dctk = connected.toolkit;
	connected.debug.print(imgTitle[1]);
	connected.debug.print(results);
	connected.debug.print(camId[1]);
	
	createItemList()

}

function onCamerasXHRStateChange()
{
    if (camerasXHR.readyState == 4) {
        if (camerasXHR.status == 200) {
            handleCamerasResult(camerasXHR.responseXML);
        }
    }
}

function startCamerasRequest()
{
	camerasXHR = new XMLHttpRequest();
	camerasXHR.open("GET", "http://api.wxbug.net/getCamerasXML.aspx?ACode="+apiKey+"&"+zipCode, true);
	camerasXHR.overrideMimeType('text/xml');
	camerasXHR.onreadystatechange = onCamerasXHRStateChange;
	camerasXHR.send(null); 
}

function createItemList(){
	var item;
	var dctk = connected.toolkit;
	for (var i = 1; i <= results; i++){
		item = itemList.addItem("id-0"+[i]);
    	item.text = i+" - "+ imgTitle[i];
		item.img.src = "http://wwc.instacam.com/instacamimg/"+camId[i]+"/"+camId[i]+"_l.jpg";
		//item.showText = true;
	}
	connected.debug.print(camId[1]);
	
}

function camPopupButtonHandler(e)
{
	var dctk = connected.toolkit;

	switch(e.buttonId)
	{
		case dcKeyEvent.BUTTON_ID_BACK:
		{
			dctk.dismissPopup();
		}
		break;

	}
}

function showCamPopup()
{
	var dctk = connected.toolkit;
	
	// create a new panel for the popup
	var camPopup = dctk.createPanelCtrl("camPopup", true);
	camPopup.width = 440;
	camPopup.height = 300;
	camPopup.outlined = true;
	camPopup.filled = true;
	camPopup.color.rgb = 0x000000;
	camPopup.border.thickness = 3;
    camPopup.border.rounded = true;
	//camPopup.color.a = 192;
	camPopup.onUnhandledKeyEvent = camPopupButtonHandler;
	
	var camTxt = dctk.createLabelCtrl("camTxt", true);
    camTxt.width = 440;
	camTxt.height = 300;
    camTxt.textColor.rgb = 0xCCCCCC;
    camTxt.fontSize = 14;
	camTxt.align = dctkLabel.TS_CENTER;
	camTxt.valign = dctkLabel.TS_MIDDLE;
    camTxt.text = "Sorry... no cameras.";
    camPopup.addChild(camTxt);
	
	itemList = dctk.createFilmstripCtrl("itemList", true);
    //itemList.setRect(20, 20, 420, 200);
	itemList.width = 440;
	itemList.height = 300;
	itemList.reflect = true;
	itemList.style = dctkIFilmstrip.FILMSTRIP_STYLE_HORIZONTAL_MIDDLE;
	itemList.scrollBarWidth = 2;
	itemList.textColor.rgb = 0xFFFFFF;
	camPopup.addChild(itemList);
	
	
	
	dctk.invokePopup(camPopup);
	
	startCamerasRequest();

}